//
//  HTVipBannerView.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/28.
//

import SwiftUI

struct HTVipBannerView: View {

  @Binding var var_selEp: HTEp!
  @Binding var var_source: String
  @Binding var var_animeInfo: HTAnime!

  @EnvironmentObject private var var_store: HTAppStore
  @State private var var_curSubscribe: HTSubscribe?
  @State private var var_introductorySub: HTSubscribe? = HTSubscribe()
  @State private var var_pushToPremium: Bool = false

  var body: some View {
    ZStack {
      NavigationLink(destination: HTPremiumScreen(var_isPush: true), isActive: $var_pushToPremium) {
        EmptyView()
      }
      
      if (var_curSubscribe == nil) {
        GeometryReader { geometry in
          HStack(alignment: .center){
            ht_vipBannerItem(iconName: "remove_ads", text1: "Remove All", text2: "Ads")
              .frame(width: geometry.size.width * 0.3 - 1)
            Spacer().frame(width: 1, height: 29).background(Color(white: 0, opacity: 0.1))
            ht_vipBannerItem(iconName: "unlock_anime", text1: "Unlock all", text2: "anime")
              .frame(width: geometry.size.width * 0.3)
            Spacer().frame(width: 1, height: 29).background(Color(white: 0, opacity: 0.1))
            VStack(spacing: 0){
              ZStack(alignment: .topLeading){
                VStack(spacing: 0){
                  Text("GET")
                    .font(.system(size: 12))
                    .foregroundColor(Color.white)
                    .fontWeight(.bold)
                  Text("PREMIUM")
                    .font(.system(size: 12))
                    .foregroundColor(Color.white)
                    .fontWeight(.bold)
                }
                .frame(width: 97, height: 40)
                .background(Color(hex: "685034"))
                .cornerRadius(4)
                Text("ONLY $\(var_introductorySub!.ht_eachPrice())/DAY")
                  .frame(height: 15)
                  .padding(.leading, 6)
                  .padding(.trailing, 6)
                  .background(Color(hex: "FF6C3C"))
                  .cornerRadius(8, corners: [.topLeft, .bottomRight])
                  .font(.system(size: 8))
                  .foregroundColor(Color.white)
                  .offset(x: 0, y: -8)
              }
              .frame(width: 97, height: 40)
              .padding(.top, 8)
            }.frame(width: geometry.size.width * 0.4 - 1)
          }
          .frame(width: geometry.size.width, height: 60)
          .background(LinearGradient(gradient: Gradient(colors: [Color(hex: "EDC391"), Color(hex: "FDDDB7")]), startPoint: .leading, endPoint: .trailing))
          .onTapGesture {
            var_pushToPremium = true
            HTNetworkManager().ht_requestForBuried(.htEnumVipSh(var_source: "13"))
            HTNetworkManager().ht_requestForBuried(.htEnumMoviePlayCl(var_movie_id: var_animeInfo!.var_id, var_movie_name: var_animeInfo!.var_title ?? "", var_kid: "32", var_source: var_source, var_eps_id: var_selEp.var_id ?? "", var_eps_name: var_selEp.var_title ?? "", var_movielist_id: "", var_secname: "", var_subtitle: "1", var_m_type: "2", var_ori_type: "1"))
          }
        }
        .frame(maxWidth: .infinity, maxHeight: 60)
      } else {
        EmptyView()
      }
    }
    .onAppear{
      var_curSubscribe = var_store.state.var_subscribeState.var_curSubscribe ?? nil
      var_introductorySub = var_store.state.var_subscribeState.var_introductorySubscribe!
    }
    .onReceive(var_store.objectWillChange) { _ in
      var_curSubscribe = var_store.state.var_subscribeState.var_curSubscribe ?? nil
      var_introductorySub = var_store.state.var_subscribeState.var_introductorySubscribe!
    }
  }
}

extension HTVipBannerView {
  func ht_vipBannerItem(iconName: String, text1: String, text2: String) -> some View {
    VStack(spacing: 0){
      Image(iconName).resizable().frame(width:20, height:20).padding(.top, 8)
      Text(text1).font(.system(size: 10)).foregroundColor(Color(hex: "685034"))
      Text(text2).font(.system(size: 10)).foregroundColor(Color(hex: "685034"))
    }
  }
}
